clear
set more off

local yearlist $yearlist
local varlist1 $varlist1

*Get data
use ${tempdir}/panel_trends_30yr_income.dta, clear
keep if race==1


*Variables for the relevant marriagemarket and childhood years
gen maryear = year2 - 10
gen chyear = year2 - 30

*Here, assign the state the match the panel controls as statefip, state of residence
*Change to bpl to match on birth place instead
gen fips = statefip

* Regressions of ever married status
egen firstcode = group(sex first year2)

gen int bpl2 = bpl
replace bpl2 = 9999 if bpl>100

gen byte foreign_born = bpl2>100 if bpl2~=.

sort year2 foreign_born

gen logoccscore_gap = logoccscore_father_spouse - logoccscore_father
gen abs_agegap = abs(agegap)

gen urb = urban==2 if urban!=.
label var urb "Urban dummy"


forvalues i = 1/4 {
	gen q`i' = qtl_score_wgt_full==`i'
	replace q`i'=. if qtl_score_wgt_full==.
}

foreach y in `yearlist' {
		gen y`y' = year2==`y'
}

forvalues i=1/4 {
	foreach y in `yearlist' {
		gen q`i'_`y' = q`i'*y`y'
	}
}

local dep_ever_married = 1
local dep_logocc_fil = 2


gen logocc_fil = logoccscore_father_spouse 
gen logoccscore_husband_temp = log(husboccscore)
gen husb_parent_qtl = qtl_score_wgt_full_spouse

egen tag_first = tag(first sex)
egen total_name_count = count(serial), by(first sex)
gsort sex -tag_first -total_name_count

forvalues i=1/5 {
	gen malename`i'=first in `i'
}

gsort -sex -tag_first -total_name_count

forvalues i=1/5 {
	gen femalename`i'=first in `i'
}


foreach s in male female {
	forvalues i=1/5 {
		gsort -`s'name`i'
		replace `s'name`i' = `s'name`i'[_n-1] if `s'name`i'==""
	}
}

gen drop1 = name_count<5
gen drop2 = femalename1==first & sex==2
gen drop3 = femalename2==first & sex==2
gen drop4 = femalename3==first & sex==2
gen drop5 = femalename4==first & sex==2
gen drop6 = femalename5==first & sex==2

cap postclose results
postfile results tag dropnum depvar spec qtile year b se using "$tempdir/reg_robust_dropnames.dta", replace

forvalues d=1/6 {
foreach var of varlist ever_married logocc_fil  {

* Ever married regressions -- no bpl controls
reg `var' `varlist1' i.age foreign_born if sex==2 & drop`d'!=1, cluster(firstcode)
gen s1_`var'1_drop`d' = e(sample)

forvalues i=2/4 {
	foreach y in `yearlist' {
		local b`i'_`y' = _b[q`i'_`y']
		local se`i'_`y' = _se[q`i'_`y']	
	}
}

forvalues i=2/4 {
	foreach y in `yearlist' {
		post results (1) (`d') (`dep_`var'') (1) (`i') (`y') (`b`i'_`y'') (`se`i'_`y'')
	}
}


* Ever married regressions -- full bpl controls
reg `var' `varlist1' i.age i.bpl2##i.year2 if sex==2 & drop`d'!=1, cluster(firstcode)
gen s2_`var'1_drop`d' = e(sample)


forvalues i=2/4 {
	foreach y in `yearlist' {
		local b`i'_`y' = _b[q`i'_`y']
		local se`i'_`y' = _se[q`i'_`y']	
	}
}


forvalues i=2/4 {
	foreach y in `yearlist' {
		post results (1) (`d') (`dep_`var'') (2) (`i') (`y') (`b`i'_`y'') (`se`i'_`y'')
	}
}

}
}

postclose results


save "${tempdir}reg_data_dropnames.dta", replace 

clear 
save "${tempdir}reg_nobs_dropnames.dta", replace empty

local dep_ever_married = 1
local dep_logocc_fil = 2


foreach x in 1 {
foreach var in ever_married logocc_fil  {
foreach spec in 1 2 {
forvalues d=1/6 {

use "${tempdir}reg_data_dropnames.dta", clear

keep if s`spec'_`var'`x'_drop`d' == 1

sort year2 first
by year2 first: gen nnames_female = _n == 1

if "`var'" == "logocc_fil" {
sort year2 spousefirst
by year2 spousefirst: gen nnames_male = _n == 1
	}
	
collapse (sum) nobs=s`spec'_`var'`x'_drop`d' nnames_*, by(year2)	

gen tag = `x'
gen depvar = `dep_`var''
gen spec = `spec'
gen dropnum=`d'

rename year2 year 

append using "${tempdir}reg_nobs_dropnames.dta"
save "${tempdir}reg_nobs_dropnames.dta", replace 

}
}
}
}

use "$tempdir/reg_robust_dropnames.dta", clear
merge m:1 tag dropnum depvar spec year using "${tempdir}reg_nobs_dropnames.dta"

export excel using "$outputdir/Regression Robustness.xlsx", sheet("dropnames", replace) firstrow(var)

erase "${tempdir}reg_data_dropnames.dta"
erase "${tempdir}reg_nobs_dropnames.dta"

